package org.elanfox.intellicommunity.intellicommunityadmin.controller.map;

import org.apache.commons.lang3.StringUtils;
import org.elanfox.intellicommunity.biz.dto.NodeDto;
import org.elanfox.intellicommunity.biz.dto.msg.LocationMsgDTO;
import org.elanfox.intellicommunity.biz.entity.resident.UserPO;
import org.elanfox.intellicommunity.biz.service.core.equipment.LocatorService;
import org.elanfox.intellicommunity.biz.service.core.msg.LocationMsgService;
import org.elanfox.intellicommunity.biz.service.core.resident.StructureService;
import org.elanfox.intellicommunity.biz.service.core.resident.UserService;
import org.elanfox.intellicommunity.biz.vo.SelectUserVO;
import org.elanfox.intellicommunity.biz.vo.UserCountVo;
import org.elanfox.intellicommunity.common.base.ApiResult;
import org.elanfox.intellicommunity.common.base.ResultCodeEnum;
import org.elanfox.intellicommunity.common.utils.LocalDateTimeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author loutianxi
 * @date 2019-12-08
 */
@Controller
@RequestMapping("/map")
public class MapController {
    @Autowired
    private StructureService structureService;
    @Autowired
    private LocatorService locatorService;
    @Autowired
    private UserService userService;
    @Autowired
    private LocationMsgService locationMsgService;

    /**
     * 跳转到打点页面
     *
     * @param model
     * @param strId
     * @return
     */
    @RequestMapping(value = {"/add/point/page"})
    public String desktop(Model model, @RequestParam(defaultValue = "1") Integer strId) {
        NodeDto nodeDto = structureService.findNode(strId);
        model.addAttribute("data", nodeDto.getAllUrl());
        model.addAttribute("strId", strId);
        model.addAttribute("strName", nodeDto.getText());
        model.addAttribute("list", locatorService.findEquipment());
        return "map/add_point";
    }

    /**
     * 获取社区树
     *
     * @return
     */
    @RequestMapping(value = "/structure/tree", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult structureTree() {
        List<NodeDto> list = structureService.findTree();
        return ApiResult.resultWith(ResultCodeEnum.SUCCESS, list);
    }

    /**
     * 获取用户列表
     *
     * @return
     */
    @RequestMapping(value = "/user/list", method = RequestMethod.GET)
    @ResponseBody
    public Map userList() {
        List<UserPO> list = userService.findDelFlagIsTrue();
        List<SelectUserVO> vos = new ArrayList<>();
        list.forEach(a -> {
            vos.add(new SelectUserVO(a));
        });
        Map map = new HashMap();
        map.put("value", vos);
        return map;
    }

    /**
     * 根据用户id获取最新一条定位消息
     *
     * @param userId
     * @return
     */
    @RequestMapping(value = "/location/msg", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult locationMsg(Integer userId) {
        LocationMsgDTO locationMsgDTO = locationMsgService.findLastByUser(userId);
        if (locationMsgDTO == null) {
            return ApiResult.resultWith(ResultCodeEnum.OPERATION_FAILED, "未查询到定位信息");
        }
        return ApiResult.resultWith(ResultCodeEnum.SUCCESS, locationMsgDTO);
    }

    @RequestMapping(value = "/track")
    public String track(Integer userId, Model model) {
        model.addAttribute("userId", userId);
        return "map/track_map";
    }

    @RequestMapping(value = "/location/msg/str", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult locationMsgByStrId(Integer strId) {
        List<Object> list = locationMsgService.findLastAllByMap(strId);
        return ApiResult.resultWith(ResultCodeEnum.SUCCESS, list);
    }

    @RequestMapping(value = "/location/msg/str/map", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult locationMsgByStrIdMap(Integer strId) {
        List<Object> list = locationMsgService.findLastAllByStrId(strId);
        return ApiResult.resultWith(ResultCodeEnum.SUCCESS, list);
    }

    /**
     * 获取用户历史路径
     *
     * @return
     */
    @RequestMapping(value = "/track/data", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult trackData(Integer userId, String startTime, String endTime) {
        LocalDateTime startLocalDateTime;
        LocalDateTime endLocalDateTime;
        if (StringUtils.isEmpty(endTime)) {
            endLocalDateTime = LocalDateTime.now();
        } else {
            endLocalDateTime = LocalDateTimeUtils.convert(endTime);
        }
        if (StringUtils.isEmpty(startTime)) {
            startLocalDateTime = endLocalDateTime.plusMinutes(-30);
        } else {
            startLocalDateTime = LocalDateTimeUtils.convert(startTime);
        }
        Map<String, List> map = locationMsgService.createTrackDto(userId, startLocalDateTime, endLocalDateTime);
        return ApiResult.resultWith(ResultCodeEnum.SUCCESS, map);
    }

    /**
     * 统计当前地图的当前人数
     *
     * @return
     */
    @RequestMapping(value = "/statistics", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult statistics(Integer structureId) {
        List<UserCountVo> list = structureService.userCount(structureId);
        return ApiResult.resultWith(ResultCodeEnum.SUCCESS, list);

    }

    @RequestMapping(value = {"/to/map/user/list"})
    public String toMapUserList(Model model, @RequestParam(defaultValue = "1") Integer strId) {
        model.addAttribute("strId", strId);
        return "map/map_resident_list";
    }
}
